package org.makcro.sms.services.chat.base.dao;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.makcro.sms.services.chat.model.Agent;
import org.makcro.sms.services.chat.model.Question;
import org.makcro.sms.services.chat.model.QuestionResponsibility;

public class QuestionResponsabilityDAO extends AbstractDAO<QuestionResponsibility> implements QuestionResponsibilityDao {

	@PersistenceContext
	EntityManager entityManager;
	
	public QuestionResponsabilityDAO() {
		super(QuestionResponsibility.class);
		// TODO Auto-generated constructor stub
	}
	
	/* (non-Javadoc)
	 * @see org.makcro.sms.services.chat.base.dao.QuestionResponsibilityDao#findByAngentAndQuestion(org.makcro.sms.services.chat.model.Agent, org.makcro.sms.services.chat.model.Question)
	 */
	@Override
	public QuestionResponsibility findByAngentAndQuestion(Agent agent, Question question) {

		return (QuestionResponsibility)entityManager.createNamedQuery("QuestionResponsibility.findByAgentAndQuestion").setParameter("agent", agent).setParameter("question", question).getSingleResult();
		
	}
	
	/* (non-Javadoc)
	 * @see org.makcro.sms.services.chat.base.dao.QuestionResponsibilityDao#findByAngentAndQuestionId(org.makcro.sms.services.chat.model.Agent, java.lang.Integer)
	 */
	@Override
	public QuestionResponsibility findByAngentAndQuestionId(Agent agent, Integer questionId) {

		return (QuestionResponsibility)entityManager.createNamedQuery("QuestionResponsibility.findByAgentAndQuestionId").setParameter("agent", agent).setParameter("questionId", questionId).getSingleResult();
		
	}
	
	/* (non-Javadoc)
	 * @see org.makcro.sms.services.chat.base.dao.QuestionResponsibilityDao#findByQuestionId(java.lang.Integer)
	 */
	@Override
	public QuestionResponsibility findByQuestionId(Integer questionId) {

		QuestionResponsibility result = null;
		try {
			result = (QuestionResponsibility) entityManager.createNamedQuery( "QuestionResponsibility.findByQuestionId" ).setParameter( "questionId", questionId ).getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		return result;
		
	}
	
	/* (non-Javadoc)
	 * @see org.makcro.sms.services.chat.base.dao.QuestionResponsibilityDao#deleteByQuestionIdAndAgent(java.lang.Integer, org.makcro.sms.services.chat.model.Agent)
	 */
	@Override
	public void deleteByQuestionIdAndAgent(Integer questionId, Agent agent){
		entityManager.createNamedQuery( "QuestionResponsibility.deleteByAgentAndQuestionId" ).setParameter( "questionId", questionId).setParameter( "agent", agent).executeUpdate();
	}
	
	
}
